package com.moco.launcher.alarm;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/* 
 * usage:  
 * DatabaseSetup.init(egActivityOrContext); 
 * DatabaseSetup.createEntry() or DatabaseSetup.getContactNames() or DatabaseSetup.getDb() 
 * DatabaseSetup.deactivate() then job done 
 */

public class Database extends SQLiteOpenHelper {
	private static Database instance = null;
	private static SQLiteDatabase database = null;

	private static final String DATABASE_NAME = "DB";
	private 	static final int DATABASE_VERSION = 1;

	public static final String ALARM_TABLE = "alarm";
	public static final String COLUMN_ALARM_ID = "_id";
	public static final String COLUMN_ALARM_ACTIVE = "alarm_active";
	public static final String COLUMN_ALARM_TIME = "alarm_time";
	public static final String COLUMN_ALARM_TONE = "alarm_tone";

	private static final String[] columns = { COLUMN_ALARM_ID, 
											COLUMN_ALARM_ACTIVE,
											COLUMN_ALARM_TIME, 
											COLUMN_ALARM_TONE, };

	public static void init(Context context) {
		if (null == instance) {
			instance = new Database(context);
		}
	}

	public static SQLiteDatabase getDatabase() {
		if (null == database) {
			database = instance.getWritableDatabase();
		}
		return database;
	}

	public static void deactivate() {
		if (null != database && database.isOpen()) {
			database.close();
		}
		database = null;
		instance = null;
	}

	public static long create(Alarm alarm) {
		ContentValues cv = new ContentValues();
		
		cv.put(COLUMN_ALARM_ACTIVE, alarm.getAlarmActive());
		cv.put(COLUMN_ALARM_TIME, alarm.getAlarmTimeString());
		cv.put(COLUMN_ALARM_TONE, alarm.getAlarmTonePath());

		return getDatabase().insert(ALARM_TABLE, null, cv);
	}

	public static int update(Alarm alarm) {
		ContentValues cv = new ContentValues();
		
		cv.put(COLUMN_ALARM_ACTIVE, alarm.getAlarmActive());
		cv.put(COLUMN_ALARM_TIME, alarm.getAlarmTimeString());
		cv.put(COLUMN_ALARM_TONE, alarm.getAlarmTonePath());

		return getDatabase().update(ALARM_TABLE, cv, "_id=" + alarm.getId(), null);
	}

	public static int deleteEntry(Alarm alarm) {
		return deleteEntry(alarm.getId());
	}

	public static int deleteEntry(int id) {
		return getDatabase().delete(ALARM_TABLE, COLUMN_ALARM_ID + "=" + id, null);
	}

	public static int deleteAll() {
		return getDatabase().delete(ALARM_TABLE, "1", null);
	}

	public static Alarm getAlarm(int id) {
		Cursor c = getDatabase().query(ALARM_TABLE, columns,
				COLUMN_ALARM_ID + "=" + id, null, null, null, null);
		Alarm alarm = null;

		if (c.moveToFirst()) {
			alarm = new Alarm();
			alarm.setId(c.getInt(1));
			alarm.setAlarmActive(c.getInt(2) == 1);
			alarm.setAlarmTime(c.getString(3));
			alarm.setAlarmTonePath(c.getString(4));
		}
		c.close();
		return alarm;
	}

	public static Cursor getCursor() {
		return getDatabase().query(ALARM_TABLE, columns, null, null, null,
				null, null);
	}

	Database(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE IF NOT EXISTS " + ALARM_TABLE + " ( "
				+ COLUMN_ALARM_ID + " INTEGER primary key autoincrement, "
				+ COLUMN_ALARM_ACTIVE + " INTEGER NOT NULL, "
				+ COLUMN_ALARM_TIME + " TEXT NOT NULL, " 
				+ COLUMN_ALARM_TONE + " TEXT NOT NULL)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + ALARM_TABLE);
		
		onCreate(db);
	}

	public static List<Alarm> getAll() {
		List<Alarm> alarms = new ArrayList<Alarm>();
		Cursor cursor = Database.getCursor();
		if (cursor.moveToFirst()) {
			do {
				Alarm alarm = new Alarm();
				alarm.setId(cursor.getInt(0));
				alarm.setAlarmActive(cursor.getInt(1) == 1);
				alarm.setAlarmTime(cursor.getString(2));
				alarm.setAlarmTonePath(cursor.getString(3));

				alarms.add(alarm);
			} while (cursor.moveToNext());
		}
		cursor.close();
		
		return alarms;
	}
}